$(function() {

// ------------------------------------------------------------------------------------------------------------------
// ----------------------------列表页面------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------

	//获取擅长领域的数据
    $.get(jumpUrl+'/lawyer/workscopes', function(data){
          
        }, "json").error(function(data) {
          var status_code = $.parseJSON(data.responseText);
          layer.msg(status_code.message);
        }).success(function(data) {
           var obj = data.data;
            var work_scope = obj;
            var src = '';
            $.each(work_scope, function(key, item) {
            	src+='<a href="javascript:void(0);" value="'+item.key+'">'+item.value+'</a>';
            });
            $(".type-box").append(src);
    });


    $(".search-input").focus(function(){  // 获得焦点
    		$(".search-input").css({"border":"1px solid #48b4ff"});
    		$(".search-icon").css("background","url("+jumpUrl+"/img/question2/searchs.png)");
    	}).blur(function(){     // 失去焦点
    		$(".search-input").css({"border":"1px solid #e8e8e8"});
    		$(".search-icon").css("background","url("+jumpUrl+"/img/question2/search.png)");
	   });

  //搜索
  $('.search-input').bind('input propertychange', function(e) {  
      runTiming(); 
  });

  //执行搜索的全局方法
  var timePromise;
  var runTiming = function(){
    if(timePromise){
        clearTimeout(timePromise);
        timePromise = undefined;
    }
    timePromise = setTimeout(function(){
        initialization();
        geturl();
    }, 2000);
    return timePromise;
  }

  /*
  不能删除，用来借鉴
  var timeoutHandler = null;
  if(timeoutHandler){
     clearTimeout(timeoutHandler);
  }
  timeoutHandler = setTimeout(function(){
     initialization();
      geturl();
  },2000);*/

  


  // 请求列表
  function get_question_list(question_list_url){
    var loading = '<div class="loading">加载中...</div>';
    $(".question-list-box").append(loading);
    $.get(question_list_url,function(data){
      },'json').error(function(data){
        var obj = $.parseJSON(data.responseText);
        // layer.msg(obj.error_messages);
      }).success(function(data){
        $(".loading").remove();
        var obj = data.data;
        var length = obj.length;
        console.info(obj);
        var src = '';
        if (obj.length>7) {
          $(".load-more").css("display","block")
        }else{
          $(".load-more").css("display","none")
        }
        
        if (obj.length>0) {
          for(var i = 0;i < obj.length; i++){
            if (obj[i].post_category_name == false) { obj[i].post_category_name = ""};
            src +='<a target="_blank" href="'+jumpUrl+'/question/'+obj[i].id+'" class="font18 colorA"><li>'
              src +='<div class="oh">'
                src +='<span class="question-title">'+obj[i].title+'</span>'
                src +='<span class="fr colorD font16">'+obj[i].post_category_name+'</span>'
              src +='</div>'
              src +='<div class="colorD font14">回答'
                src +='<span class="colorC"> '+obj[i].comments_count+'</span> '
                src +='<span class="font16">• </span>收藏'
                src +='<span class="colorC"> '+obj[i].collects_count+'</span>'
                src +='<span style="margin-left: 20px;">'+obj[i].difference_date+'</span>'
              src +='</div>'
            src +='</li></a>'
          }
          $(".question-list-box").append(src);
        }else{
        src ='<li class="textac"><span class="noData2"></span></li>';
        $(".question-list-box").append(src);
      }
      });
  }

// 初始化
  function initialization(){
    $(".question-list-box li").remove();
    $(".load-more").attr("data-id",1);
  }
  // 请求的url
  geturl();
  function geturl(){
   var post_category = $(".type-box a.cuB").attr("value");
   var params = $(".search-input").val();
   var index = $(".load-more").attr("data-id");
   var oeder_params = $(".type-nav2 .cuB").attr("data-id");
   if (post_category == undefined) {post_category=""};
   if (oeder_params == undefined) {oeder_params=""};
   if (index == "1") {index = ""};

   var paramss = [];
      paramss.push('type=question');
      paramss.push('size=8');
      if (!post_category=="") {
      paramss.push("post_category="+post_category);
      }
      if (!oeder_params=="") {
        paramss.push("order="+oeder_params);
      }
      if (!params=="") {
        paramss.push("q="+params);
      }
      if (!index=="") {
        paramss.push("page="+index);
      }
      paramss = paramss.join('&');
      var thisUrl = location.href.split("?")[0]+'?'+paramss;
      window.history.pushState('obj', document.title, thisUrl);

    var question_list_url= apitest+"/question?type=question&post_category="+post_category+"&order="+oeder_params+"&q="+params+"&size=8&page="+index;
    console.info(question_list_url)
    get_question_list(question_list_url);
  }
    // 类别搜索
    $(document).on("click",".type-box a",function(){
      var type_box = $(this).attr("class");
      if (type_box !== "cuB") {
        $(".type-box a").removeClass("cuB");
        $(this).addClass("cuB");
      }else{
        $(".type-box a").removeClass("cuB");
      }
      initialization();
      geturl();
    });
     // 收藏最多
    $(document).on("click",".collectMax",function(post_category){
      var collectMax = $(".collectMax.cuB").val();
      if (collectMax == undefined) {
        $(".collectMax").addClass("cuB");
        $(".replyMax").removeClass("cuB");
      }else{
        $(".collectMax").removeClass("cuB");
      }
      initialization();
      geturl();
    });

    // 回答最多
    $(document).on("click",".replyMax",function(post_category){
      var replyMax = $(".replyMax.cuB").val();
      if (replyMax == undefined) {
        $(".collectMax").removeClass("cuB");
        $(".replyMax").addClass("cuB");
      }else{
        $(".replyMax").removeClass("cuB");
      }
      
      initialization();
      geturl();
    });

     // 加载更多
    $(document).on("click",".load-more",function(e){
      index = parseInt($(".load-more").attr("data-id"))+1;
      $(".load-more").attr("data-id",index);
      geturl();
    });

    // 点击搜索图标
    $(".search-icon").click(function(){
      initialization();
      geturl();
    });
  // 点击搜索图标
    $(".quiz").click(function(){
      if(login_isLawyer_id == "" || login_isLawyer_id == null){
        window.location.href = jumpUrl+"/question/public/commit";
        return false
      }else{
        layer.msg("律师不能发布问题！");
      }
    });

  // 点击搜索
  $(document).on("click","#subSearch",function(e){
      var keyword = $("#keyword").val();
      $(".js-appendContxt").empty();
      if (keyword != null && keyword.length != 0) {
        params ='&q='+keyword;
      }else{
        params =''
      }
      // window.location.href="{{ Config::get('app.url') }}/question/create?"+params;
      getAllUrl(params);
    });

  // enter按键搜索
  var j = 0;
    document.onkeydown = function(e) {
        var keyword = $("#keyword").val();
        var params ='q='+keyword;
        var e = event || window.event || arguments.callee.caller.arguments[0],
         keycode = e.which || e.keyCode;
         if (keycode==13) {
            if (keyword != null && keyword.length != 0) {
            j++;
                if(!j==0){
                  var keyword = $("#keyword").val();
                  $(".js-appendContxt").empty();
                  if (keyword != null && keyword.length != 0) {
                    params ='&q='+keyword;
                  }else{
                    params =''
                  }
                  getAllUrl(params);
                }
            } 
            return false; 
         }else{
            return true;
         }
         stopDefault(e);
    }
})
